package com.szqhyxj.youxiangju.biz.merchantHome.plan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.PlanStatus;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 历史计划
 */
public class PlanHistoryActivity extends BaseActivity implements PlansContract.View, NavigationBar.NavigationBarListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.execution)
    RadioButton execution;
    @BindView(R.id.pause)
    RadioButton pause;
    @BindView(R.id.paln)
    RadioGroup paln;
    @BindView(R.id.fragment)
    FrameLayout fragment;
    private PlanListFragment successFragment;
    private PlanListFragment executionFragment;

    public static void start(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, PlanHistoryActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_list);
        ButterKnife.bind(this);
        initDatat();
        initEvent();

        navigationBar.setNavigationBarListener(this);
    }

    private void initDatat(){
        successFragment = PlanListFragment.newInstance(PlanStatus.SUCCESS.getCode());
        executionFragment = PlanListFragment.newInstance(PlanStatus.FAIL.getCode());
        successFragment.setSetListSize(new PlanListFragment.SetListSize() {
            @Override
            public void setListSize(int size) {
                execution.setText("正常结束 ("+size+")");
            }
        });
        executionFragment.setSetListSize(new PlanListFragment.SetListSize() {
            @Override
            public void setListSize(int size) {
                pause.setText("异常结束 ("+size+")");
            }
        });

        FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先添加再隐藏起来
        transaction.add(R.id.fragment, successFragment);
        transaction.hide(successFragment);
        transaction.add(R.id.fragment, executionFragment);
        transaction.hide(executionFragment);
        // 默认显示第一个
        transaction.show(successFragment);
        // 提交事务
        transaction.commit();


    }

    private void initEvent(){
    }

    @Override
    public void showNormalPaln(){
        getSupportFragmentManager().beginTransaction().hide(executionFragment).show(successFragment).commit();
    }

    @Override
    public void showErrPlan(){

        getSupportFragmentManager().beginTransaction().hide(successFragment).show(executionFragment).commit();
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }

    @OnClick({R.id.execution, R.id.pause})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.execution:
                showNormalPaln();
                break;
            case R.id.pause:
                showErrPlan();
                break;
        }
    }
}
